package cn.edu.henu.team.fragments;

import static cn.bmob.v3.Bmob.getApplicationContext;

import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import org.song.videoplayer.DemoQSVideoView;
import org.song.videoplayer.IVideoPlayer;
import org.song.videoplayer.PlayListener;
import org.song.videoplayer.QSVideo;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.edu.henu.team.R;
import cn.edu.henu.team.adapter.VideoAdapter;
import cn.edu.henu.team.base.BaseFragment;
import cn.edu.henu.team.bean.article_brief;
import cn.edu.henu.team.bean.video;

public class fragment1_video extends BaseFragment{

    private ListView mlistView;
    private ArrayList<video> mData = new ArrayList<>();
    private VideoAdapter madapter;
    private SmartRefreshLayout refreshSrl;

    public static Fragment newInstance() {
        fragment1_video fragment = new fragment1_video();
        return fragment;
    }

    @Override
    protected void clickEvents() {

    }

    @Override
    protected int initLayout()
    {
        return R.layout.fragment_1_video;
    }

    @Override
    protected void initView() {
        mlistView = mRootView.findViewById(R.id.vlistview);
        refreshSrl = mRootView.findViewById(R.id.refresh_video_srl);
        //刷新
        refreshSrl.setRefreshHeader(new ClassicsHeader(getApplicationContext()));
        refreshSrl.setRefreshFooter(new ClassicsFooter(getApplicationContext()));


        refreshSrl.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                refreshSrl.finishRefresh(1000/*,false*/);
                initData();
            }
        });
        refreshSrl.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadMore(1000/*,false*/);//传入false表示加载失败
            }
        });
    }

    @Override
    protected void initData() {
        mData.clear();
        BmobQuery<video> bmobQuery = new BmobQuery("video");
        bmobQuery.order("-createdAt");
        bmobQuery.findObjects(new FindListener<video>() {
            @Override
            public void done(List<video> list, BmobException e) {
                if (e==null) {
                    if (list != null) {
                        for (video a:list)
                        {
                            mData.add(a);
                            madapter = new VideoAdapter(mData,getContext());
                            mlistView.setAdapter(madapter);
                        }
                    }
                }
            }
        });

    }
}
